import logging
from .load_config import config

# 设置日志工具
logger = logging.getLogger("mqRPC")
log_formatter = logging.Formatter("%(asctime)s %(name)s[%(levelname)s]: %(message)s")
# two handler 就是日志目的地
fh = None
if config["log_file"]:
    fh = logging.FileHandler(config["log_file"])
    fh.setFormatter(log_formatter)
    # 添加到logger中
    logger.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(log_formatter)
logger.addHandler(sh)
# 调试设置不同的日志打印级别
if config.getboolean("debug"):
    logger.setLevel(logging.DEBUG)
    if fh:
        fh.setLevel(logging.DEBUG)
    sh.setLevel(logging.DEBUG)
else:
    logger.setLevel(logging.INFO)
    if fh:
        fh.setLevel(logging.INFO)
    sh.setLevel(logging.INFO)

